<?php

namespace app\user\controller;

use think\Controller;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Request;
use app\user\model\Question as modelQuestion;
use app\user\model\User as modelUser;


class Findpassword extends Controller
{
    public function index()
    {
        return $this->fetch("verifyAccount");
    }

    public function way()
    {
        $has_question = 1;
        $username = Request::post('UserName');
        Session::set('find_username', $username);
        
        $question_list = modelQuestion::where('username', $username)->select();
        if ($question_list->isEmpty()) {
            $has_question = 0;
        }

        $this->assign('has_question', $has_question);
        return $this->fetch("verifyWay");
    }

    public function mail()
    {
        $username = Session::get('find_username');
        $find_user = modelUser::where('name', $username)->find();
        
        $this->assign('find_name', $username);
        $this->assign('find_email', $find_user->email);
        return $this->fetch("verifyMail");
    }

    public function reset_email()
    {
        $code = Request::post('code');
        $email_code = Session::get('email_code');
        if ($code != $email_code) {
            Session::delete('email_code');
            echo "<script> alert('验证码错误！');</script>";
            echo "<script> location.assign('/user/Findpassword/mail');</script>";
        } else {
            $username = Session::get('find_username');
            $find_user = modelUser::where('name', $username)->find();
            $newpass = Request::post('newpass');
            $find_user->pass = $newpass;
            $find_user->save();
            Session::delete('email_code');
            Session::delete('find_username');
            
            echo "<script> alert('重置密码成功！');</script>";
            echo "<script> location.assign('/login');</script>";
        }
    }

    public function question()
    {
        $username = Session::get('find_username');
        $this->assign('find_name', $username);
        return $this->fetch("verifyQuestion");
    }

    public function reset_question()
    {
        $username = Session::get('find_username');
        $questionlist = modelQuestion::where('username',$username)->order('id')->select();

        $question1 = Request::post('question1');
        $answer1 = Request::post('answer1');
        if($question1 == $questionlist[0]->question && $answer1 == $questionlist[0]->answer)
        {
            $question2 = Request::post('question2');
            $answer2 = Request::post('answer2');
            if($question2 == $questionlist[1]->question && $answer2 == $questionlist[1]->answer)
            {
                $find_user = modelUser::where('name', $username)->find();
                $newpass = Request::post('newpass');
                $find_user->pass = $newpass;
                $find_user->save();
                Session::delete('find_username');
                echo "<script> alert('重置密码成功！');</script>";
                echo "<script> location.assign('/login');</script>";
                
            }else{
                echo "<script> alert('密保问题错误！');</script>";
                echo "<script> location.assign('/user/Findpassword/way');</script>";
            }
        }else{
            echo "<script> alert('密保问题错误！');</script>";
            echo "<script> location.assign('/user/Findpassword/way');</script>";
        }
        
        

    }

}
